package models;
import play.*;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;

import play.data.validation.Required;
import play.db.jpa.Model;

/**
 * @author Mostafa Ali
 * 
 * */
@Entity
public class Request extends Model{

	@ManyToOne
	public User sendingUser;
	@ManyToOne
	public EEntity entity;
	@ManyToOne
	public Organization organization;
	@ManyToOne
	public User recievingUser;
	@Required
	public Date invitationDate;
	@Required
	public String description;
	
	public Request(User sendingUser, EEntity entity, Organization organization, 
			User recievingUser, Date invitationDate, String description){
		this.sendingUser = sendingUser;
		this.entity = entity;
		this.organization = organization;
		this.recievingUser = recievingUser;
		this.invitationDate = invitationDate;
		this.description = description;
	}
		/**
		 * @author Ahmed Mahmoud
		 * @return
		 */
		public boolean cascadeDelete(){
			sendingUser.sentRequests.remove(this);
			sendingUser.save();
			entity.entityRequests.remove(this);
			entity.save();
			organization.orgRequests.remove(this);
			organization.save();
			recievingUser.invitations.remove(this);
			recievingUser.save();
			this.delete();
			return true;
		}

	public Request(User sendingUser, Organization organization, 
			User recievingUser, Date invitationDate, String description){
		this.sendingUser = sendingUser;
		this.organization = organization;
		this.recievingUser = recievingUser;
		this.invitationDate = invitationDate;
		this.description = description;
	}
}
